
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Future · Dart编程指南</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="arcticfox">
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="异步编程之Stream.html" />
    
    
    <link rel="prev" href="异步编程.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="http://www.bczl.xyz/" target="_blank" class="custom-link">主页</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="基础篇.html">
            
                <a href="基础篇.html">
            
                    
                    基础篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="环境搭建.html">
            
                <a href="环境搭建.html">
            
                    
                    环境搭建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="简单语法.html">
            
                <a href="简单语法.html">
            
                    
                    简单语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="进阶篇.html">
            
                <a href="进阶篇.html">
            
                    
                    进阶篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="面向对象.html">
            
                <a href="面向对象.html">
            
                    
                    面向对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="泛型.html">
            
                <a href="泛型.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="枚举.html">
            
                <a href="枚举.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="库与导入.html">
            
                <a href="库与导入.html">
            
                    
                    库与导入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="异步编程.html">
            
                <a href="异步编程.html">
            
                    
                    异步编程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.3.5.1" data-path="异步编程之Future.html">
            
                <a href="异步编程之Future.html">
            
                    
                    Future
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5.2" data-path="异步编程之Stream.html">
            
                <a href="异步编程之Stream.html">
            
                    
                    Stream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5.3" data-path="异步编程之Isolate.html">
            
                <a href="异步编程之Isolate.html">
            
                    
                    Isolate
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="异常处理.html">
            
                <a href="异常处理.html">
            
                    
                    异常处理与 Zone
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="生成器.html">
            
                <a href="生成器.html">
            
                    
                    生成器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="标准流与文件.html">
            
                <a href="标准流与文件.html">
            
                    
                    标准流与文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="运算符.html">
            
                <a href="运算符.html">
            
                    
                    附：运算符
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="常用标准库.html">
            
                <a href="常用标准库.html">
            
                    
                    常用标准库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="字符串.html">
            
                <a href="字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="集合.html">
            
                <a href="集合.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="URI处理.html">
            
                <a href="URI处理.html">
            
                    
                    URI处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="日期和时间.html">
            
                <a href="日期和时间.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="环境及外部调用.html">
            
                <a href="环境及外部调用.html">
            
                    
                    环境及外部调用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="其他.html">
            
                <a href="其他.html">
            
                    
                    其他
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="实战篇.html">
            
                <a href="实战篇.html">
            
                    
                    实战篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="网络编程.html">
            
                <a href="网络编程.html">
            
                    
                    网络编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="爬虫实战.html">
            
                <a href="爬虫实战.html">
            
                    
                    爬虫实战
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="HTTP服务器.html">
            
                <a href="HTTP服务器.html">
            
                    
                    HTTP服务器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="WebSocket服务器.html">
            
                <a href="WebSocket服务器.html">
            
                    
                    WebSocket服务器
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    附录
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="dart2.14.html">
            
                <a href="dart2.14.html">
            
                    
                    升级到Dart 2.14
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="http://www.bczl.xyz/" target="blank" class="gitbook-link">
            编程之路
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >Future</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><ul><li><span class="title-icon "></span><a href="#future-&#x8BE6;&#x89E3;"><b></b>Future &#x8BE6;&#x89E3;</a></li><ul><li><span class="title-icon "></span><a href="#&#x521B;&#x5EFA;-future"><b></b>&#x521B;&#x5EFA; Future</a></li><li><span class="title-icon "></span><a href="#&#x6CE8;&#x518C;&#x56DE;&#x8C03;"><b></b>&#x6CE8;&#x518C;&#x56DE;&#x8C03;</a></li></ul><li><span class="title-icon "></span><a href="#async-&#x548C;-await"><b></b>async &#x548C; await</a></li></ul></ul></div><a href="#" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><!-- ex_nolevel -->
<h2 id="future-&#x8BE6;&#x89E3;"><a name="future-&#x8BE6;&#x89E3;" class="anchor-navigation-ex-anchor" href="#future-&#x8BE6;&#x89E3;"><i class="fa fa-link" aria-hidden="true"></i></a>Future &#x8BE6;&#x89E3;</h2>
<p>Future&#x7C7B;&#x662F;&#x5BF9;&#x672A;&#x6765;&#x7ED3;&#x679C;&#x7684;&#x4E00;&#x4E2A;&#x4EE3;&#x7406;&#xFF0C;&#x5B83;&#x8FD4;&#x56DE;&#x7684;&#x5E76;&#x4E0D;&#x662F;&#x88AB;&#x8C03;&#x7528;&#x7684;&#x4EFB;&#x52A1;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x3002;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">void</span>  myTask(){
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;this is my task&quot;</span>);
}

<span class="hljs-keyword">void</span>  main() {
    Future fut = <span class="hljs-keyword">new</span>  Future(myTask);
}
</code></pre>
<p>&#x5982;&#x4E0A;&#x4EE3;&#x7801;&#xFF0C;<code>Future</code>&#x7C7B;&#x5B9E;&#x4F8B;<code>fut</code>&#x5E76;&#x4E0D;&#x662F;&#x51FD;&#x6570;<code>myTask</code>&#x7684;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x5B83;&#x53EA;&#x662F;&#x4EE3;&#x7406;&#x4E86;<code>myTask</code>&#x51FD;&#x6570;&#xFF0C;&#x5C01;&#x88C5;&#x4E86;&#x8BE5;&#x4EFB;&#x52A1;&#x7684;&#x6267;&#x884C;&#x72B6;&#x6001;&#x3002;</p>
<h3 id="&#x521B;&#x5EFA;-future"><a name="&#x521B;&#x5EFA;-future" class="anchor-navigation-ex-anchor" href="#&#x521B;&#x5EFA;-future"><i class="fa fa-link" aria-hidden="true"></i></a>&#x521B;&#x5EFA; Future</h3>
<p><code>Future</code>&#x7684;&#x51E0;&#x79CD;&#x521B;&#x5EFA;&#x65B9;&#x6CD5;</p>
<ul>
<li><code>Future()</code></li>
<li><code>Future.microtask()</code></li>
<li><code>Future.sync()</code></li>
<li><code>Future.value()</code></li>
<li><code>Future.delayed()</code></li>
<li><code>Future.error()</code></li>
</ul>
<p>&#x5176;&#x4E2D;<code>sync</code>&#x662F;&#x540C;&#x6B65;&#x65B9;&#x6CD5;&#xFF0C;&#x4EFB;&#x52A1;&#x4F1A;&#x88AB;&#x7ACB;&#x5373;&#x6267;&#x884C;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span>  <span class="hljs-string">&apos;dart:async&apos;</span>;

<span class="hljs-keyword">void</span>  main() {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;main start&quot;</span>);

<span class="hljs-keyword">new</span>  Future.<span class="hljs-keyword">sync</span>((){
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;sync task&quot;</span>);
});

<span class="hljs-keyword">new</span>  Future((){
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;async task&quot;</span>);
});

    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;main stop&quot;</span>);
}
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code>main start
sync task
main stop
async task
</code></pre><h3 id="&#x6CE8;&#x518C;&#x56DE;&#x8C03;"><a name="&#x6CE8;&#x518C;&#x56DE;&#x8C03;" class="anchor-navigation-ex-anchor" href="#&#x6CE8;&#x518C;&#x56DE;&#x8C03;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6CE8;&#x518C;&#x56DE;&#x8C03;</h3>
<p>&#x5F53;<code>Future</code>&#x4E2D;&#x7684;&#x4EFB;&#x52A1;&#x5B8C;&#x6210;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x5F80;&#x5F80;&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x56DE;&#x8C03;&#xFF0C;&#x8FD9;&#x4E2A;&#x56DE;&#x8C03;&#x7ACB;&#x5373;&#x6267;&#x884C;&#xFF0C;&#x4E0D;&#x4F1A;&#x88AB;&#x6DFB;&#x52A0;&#x5230;&#x4E8B;&#x4EF6;&#x961F;&#x5217;&#x3002;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:async&apos;</span>;

<span class="hljs-keyword">void</span> main() {
  <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;main start&quot;</span>);


  Future fut =<span class="hljs-keyword">new</span> Future.value(<span class="hljs-number">18</span>);
  <span class="hljs-comment">// &#x4F7F;&#x7528;then&#x6CE8;&#x518C;&#x56DE;&#x8C03;</span>
  fut.then((res){
    <span class="hljs-built_in">print</span>(res);
  });

 <span class="hljs-comment">// &#x94FE;&#x5F0F;&#x8C03;&#x7528;&#xFF0C;&#x53EF;&#x4EE5;&#x8DDF;&#x591A;&#x4E2A;then&#xFF0C;&#x6CE8;&#x518C;&#x591A;&#x4E2A;&#x56DE;&#x8C03;</span>
  <span class="hljs-keyword">new</span> Future((){
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;async task&quot;</span>);
  }).then((res){
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;async task complete&quot;</span>);
  }).then((res){
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;async task after&quot;</span>);
  });

  <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;main stop&quot;</span>);
}
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code>main start
main stop
18
async task
async task complete
async task after
</code></pre><p>&#x9664;&#x4E86;<code>then</code>&#x65B9;&#x6CD5;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>catchError</code>&#x6765;&#x5904;&#x7406;&#x5F02;&#x5E38;&#xFF0C;&#x5982;&#x4E0B;</p>
<pre><code class="lang-dart">  <span class="hljs-keyword">new</span> Future((){
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;async task&quot;</span>);
  }).then((res){
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;async task complete&quot;</span>);
  }).catchError((e){
    <span class="hljs-built_in">print</span>(e);
  });
</code></pre>
<p>&#x8FD8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<strong>&#x9759;&#x6001;&#x65B9;&#x6CD5;</strong><code>wait</code> &#x7B49;&#x5F85;&#x591A;&#x4E2A;&#x4EFB;&#x52A1;&#x5168;&#x90E8;&#x5B8C;&#x6210;&#x540E;&#x56DE;&#x8C03;&#x3002;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:async&apos;</span>;

<span class="hljs-keyword">void</span> main() {
  <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;main start&quot;</span>);

  Future task1 = <span class="hljs-keyword">new</span> Future((){
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;task 1&quot;</span>);
    <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
  });

  Future task2 = <span class="hljs-keyword">new</span> Future((){
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;task 2&quot;</span>);
    <span class="hljs-keyword">return</span> <span class="hljs-number">2</span>;
  });

  Future task3 = <span class="hljs-keyword">new</span> Future((){
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;task 3&quot;</span>);
    <span class="hljs-keyword">return</span> <span class="hljs-number">3</span>;
  });

  Future fut = Future.wait([task1, task2, task3]);
  fut.then((responses){
    <span class="hljs-built_in">print</span>(responses);
  });

  <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;main stop&quot;</span>);
}
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code>main start
main stop
task 1
task 2
task 3
[1, 2, 3]
</code></pre><p>&#x5982;&#x4E0A;&#xFF0C;<code>wait</code>&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x7684;<code>Future</code>&#xFF0C;&#x5F53;&#x6DFB;&#x52A0;&#x7684;&#x6240;&#x6709;<code>Future</code>&#x5B8C;&#x6210;&#x65F6;&#xFF0C;&#x5728;&#x65B0;&#x7684;<code>Future</code>&#x6CE8;&#x518C;&#x7684;&#x56DE;&#x8C03;&#x5C06;&#x88AB;&#x6267;&#x884C;&#x3002;</p>
<h2 id="async-&#x548C;-await"><a name="async-&#x548C;-await" class="anchor-navigation-ex-anchor" href="#async-&#x548C;-await"><i class="fa fa-link" aria-hidden="true"></i></a>async &#x548C; await</h2>
<p>&#x5728;Dart1.9&#x4E2D;&#x52A0;&#x5165;&#x4E86;<code>async</code>&#x548C;<code>await</code>&#x5173;&#x952E;&#x5B57;&#xFF0C;&#x6709;&#x4E86;&#x8FD9;&#x4E24;&#x4E2A;&#x5173;&#x952E;&#x5B57;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x66F4;&#x7B80;&#x6D01;&#x7684;&#x7F16;&#x5199;&#x5F02;&#x6B65;&#x4EE3;&#x7801;&#xFF0C;&#x800C;&#x4E0D;&#x9700;&#x8981;&#x8C03;&#x7528;<code>Future</code>&#x76F8;&#x5173;&#x7684;API</p>
<p>&#x5C06; <code>async</code> &#x5173;&#x952E;&#x5B57;&#x4F5C;&#x4E3A;&#x65B9;&#x6CD5;&#x58F0;&#x660E;&#x7684;&#x540E;&#x7F00;&#x65F6;&#xFF0C;&#x5177;&#x6709;&#x5982;&#x4E0B;&#x610F;&#x4E49;</p>
<ul>
<li>&#x88AB;&#x4FEE;&#x9970;&#x7684;&#x65B9;&#x6CD5;&#x4F1A;&#x5C06;&#x4E00;&#x4E2A; <code>Future</code> &#x5BF9;&#x8C61;&#x4F5C;&#x4E3A;&#x8FD4;&#x56DE;&#x503C;</li>
<li>&#x8BE5;&#x65B9;&#x6CD5;&#x4F1A;<strong>&#x540C;&#x6B65;</strong>&#x6267;&#x884C;&#x5176;&#x4E2D;&#x7684;&#x65B9;&#x6CD5;&#x7684;&#x4EE3;&#x7801;&#x76F4;&#x5230;<strong>&#x7B2C;&#x4E00;&#x4E2A; await &#x5173;&#x952E;&#x5B57;</strong>&#xFF0C;&#x7136;&#x540E;&#x5B83;&#x6682;&#x505C;&#x8BE5;&#x65B9;&#x6CD5;&#x5176;&#x4ED6;&#x90E8;&#x5206;&#x7684;&#x6267;&#x884C;&#xFF1B;</li>
<li>&#x4E00;&#x65E6;&#x7531; <strong>await</strong> &#x5173;&#x952E;&#x5B57;&#x5F15;&#x7528;&#x7684; <strong>Future</strong> &#x4EFB;&#x52A1;&#x6267;&#x884C;&#x5B8C;&#x6210;&#xFF0C;<strong>await</strong>&#x7684;&#x4E0B;&#x4E00;&#x884C;&#x4EE3;&#x7801;&#x5C06;&#x7ACB;&#x5373;&#x6267;&#x884C;&#x3002;</li>
</ul>
<pre><code class="lang-dart"><span class="hljs-comment">// &#x5BFC;&#x5165;io&#x5E93;&#xFF0C;&#x8C03;&#x7528;sleep&#x51FD;&#x6570;</span>
<span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:io&apos;</span>;

<span class="hljs-comment">// &#x6A21;&#x62DF;&#x8017;&#x65F6;&#x64CD;&#x4F5C;&#xFF0C;&#x8C03;&#x7528;sleep&#x51FD;&#x6570;&#x7761;&#x7720;2&#x79D2;</span>
doTask() <span class="hljs-keyword">async</span>{
  <span class="hljs-keyword">await</span> sleep(<span class="hljs-keyword">const</span> <span class="hljs-built_in">Duration</span>(seconds:<span class="hljs-number">2</span>));
  <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;Ok&quot;</span>;
}

<span class="hljs-comment">// &#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x7528;&#x4E8E;&#x5305;&#x88C5;</span>
test() <span class="hljs-keyword">async</span> {
  <span class="hljs-keyword">var</span> r = <span class="hljs-keyword">await</span> doTask();
  <span class="hljs-built_in">print</span>(r);
}

<span class="hljs-keyword">void</span> main(){
  <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;main start&quot;</span>);
  test();
  <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;main end&quot;</span>);
}
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code>main start
main end
Ok
</code></pre><p>&#x9700;&#x8981;&#x6CE8;&#x610F;&#xFF0C;<strong>async</strong> &#x4E0D;&#x662F;&#x5E76;&#x884C;&#x6267;&#x884C;&#xFF0C;&#x5B83;&#x662F;&#x9075;&#x5FAA;Dart <strong>&#x4E8B;&#x4EF6;&#x5FAA;&#x73AF;</strong>&#x89C4;&#x5219;&#x6765;&#x6267;&#x884C;&#x7684;&#xFF0C;&#x5B83;&#x4EC5;&#x4EC5;&#x662F;&#x4E00;&#x4E2A;&#x8BED;&#x6CD5;&#x7CD6;&#xFF0C;&#x7B80;&#x5316;<code>Future API</code>&#x7684;&#x4F7F;&#x7528;&#x3002;</p>
<p>&#x53C2;&#x8003;&#x8D44;&#x6599;&#xFF1A;
<a href="https://webdev.dartlang.org/articles/performance/event-loop" target="_blank">Dart &#x6587;&#x6863;</a></p>
<hr>
<p><strong>&#x516C;&#x4F17;&#x53F7;&#x201C;&#x7F16;&#x7A0B;&#x4E4B;&#x8DEF;&#x4ECE;0&#x5230;1&#x201D;</strong></p>
<p><img src="https://gitee.com/arcticfox1919/ImageHosting/raw/master/img/20190301102949549.jpg" alt="20190301102949549"> </p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; Arcticfox 2020 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x6587;&#x6863;&#x4FEE;&#x8BA2;&#x4E8E;&#xFF1A;
2021-03-18 13:34:29
</span></footer>
<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="异步编程.html" class="navigation navigation-prev " aria-label="Previous page: 异步编程">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="异步编程之Stream.html" class="navigation navigation-next " aria-label="Next page: Stream">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Future","level":"1.3.5.1","depth":3,"next":{"title":"Stream","level":"1.3.5.2","depth":3,"path":"异步编程之Stream.md","ref":"异步编程之Stream.md","articles":[]},"previous":{"title":"异步编程","level":"1.3.5","depth":2,"path":"异步编程.md","ref":"异步编程.md","articles":[{"title":"Future","level":"1.3.5.1","depth":3,"path":"异步编程之Future.md","ref":"异步编程之Future.md","articles":[]},{"title":"Stream","level":"1.3.5.2","depth":3,"path":"异步编程之Stream.md","ref":"异步编程之Stream.md","articles":[]},{"title":"Isolate","level":"1.3.5.3","depth":3,"path":"异步编程之Isolate.md","ref":"异步编程之Isolate.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":["anchor-navigation-ex","expandable-chapters-small","-lunr","-search","search-pro","-sharing","github","expandable-chapters","pageview-count","theme-comscore","tbfed-pagefooter","popup","custom-favicon"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy Arcticfox 2020","modify_label":"文档修订于：","modify_format":"YYYY-MM-DD HH:mm:ss"},"github":{"url":"https://github.com/arcticfox1919"},"search-pro":{},"popup":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":true},"favicon":"logo.ico","theme-comscore":{},"pageview-count":{},"custom-favicon":{},"expandable-chapters-small":{},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","author":"arcticfox","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Dart编程指南","language":"zh-hans","links":{"sidebar":{"主页":"http://www.bczl.xyz/"}},"gitbook":"*"},"file":{"path":"异步编程之Future.md","mtime":"2021-03-18T05:34:29.723Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-10-18T14:58:03.414Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

